# coding: utf-8
import os
import sys
import traceback

from loguru import logger
from PySide6.QtCore import Qt, QLocale, QTranslator
from PySide6.QtWidgets import QApplication, QStackedWidget
from qfluentwidgets import FluentTranslator

from app.views import MainWindow
from app.common import cfg, signalBus

if cfg.get(cfg.dpiScale) != "Auto":
    os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
    os.environ["QT_SCALE_FACTOR"] = str(cfg.get(cfg.dpiScale))


def exception_hook(exception: BaseException, value, tb):
    """ 异常回调函数 """
    message = '\n'.join([''.join(traceback.format_tb(tb)),
                         '{0}: {1}'.format(exception.__name__, value)])
    logger.error(message)
    signalBus.sysMsgSignal.emit(message)


sys.excepthook = exception_hook
# create application
app = QApplication(sys.argv)
app.setAttribute(Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings)
app.setAttribute(Qt.ApplicationAttribute.AA_UseSoftwareOpenGL)

# internationalization
locale = QLocale(QLocale.Language.Chinese, QLocale.Country.China)
translator = FluentTranslator(locale)
galleryTranslator = QTranslator()
app.installTranslator(translator)

window = MainWindow()
window.show()

sys.exit(app.exec())
